package cn.AssassinG.ScsyERP.Statistics.core.biz.impl;

import cn.AssassinG.ScsyERP.Statistics.core.biz.YearlyStatisticsBiz;
import cn.AssassinG.ScsyERP.Statistics.core.dao.YearlyStatisticsDao;
import cn.AssassinG.ScsyERP.Statistics.facade.entity.MonthlyStatistics;
import cn.AssassinG.ScsyERP.Statistics.facade.entity.YearlyStatistics;
import cn.AssassinG.ScsyERP.Statistics.facade.enums.ObjectType;
import cn.AssassinG.ScsyERP.Statistics.facade.exceptions.YearlyStatisticsBizException;
import cn.AssassinG.ScsyERP.common.core.biz.impl.BaseBizImpl;
import cn.AssassinG.ScsyERP.common.core.dao.BaseDao;
import cn.AssassinG.ScsyERP.common.utils.ValidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("OutStorageFormBiz")
public class YearlyStatisticsBizImpl extends BaseBizImpl<YearlyStatistics> implements YearlyStatisticsBiz {
    @Autowired
    private YearlyStatisticsDao monthlyStatisticsDao;
    protected BaseDao<YearlyStatistics> getDao() {
        return this.monthlyStatisticsDao;
    }

    @Override
    @Transactional
    public Long create(YearlyStatistics yearlyStatistics) {
        if(yearlyStatistics.getInStorageAmount() == null){
            yearlyStatistics.setInStorageAmount(0L);
        }
        if(yearlyStatistics.getOutStorageAmount() == null){
            yearlyStatistics.setOutStorageAmount(0L);
        }
        if(yearlyStatistics.getProjectAmount() == null){
            yearlyStatistics.setProjectAmount(0L);
        }
        ValidUtils.ValidationWithExp(yearlyStatistics);
        Long id = getDao().insert(yearlyStatistics);
        return id;
    }

    /**
     * @param entityId
     * @param paramMap 年度统计信息字段(inStorageAmount, outStorageAmount, projectAmount)
     */
    @Transactional
    public void updateByMap(Long entityId, Map<String, String> paramMap) {
        if (entityId == null) {
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_PARAMS_ILLEGAL, "年度统计信息主键不能为空");
        }
        YearlyStatistics yearlyStatistics = this.getById(entityId);
        if (yearlyStatistics == null || yearlyStatistics.getIfDeleted()) {
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_NOSUIT_RESULT, "没有符合条件的年度统计信息，entityId: %d", entityId);
        }
        try {
            Long inStorageAmount = paramMap.get("inStorageAmount") == null ? null : Long.valueOf(paramMap.get("inStorageAmount"));
            Long outStorageAmount = paramMap.get("outStorageAmount") == null ? null : Long.valueOf(paramMap.get("outStorageAmount"));
            Long projectAmount = paramMap.get("projectAmount") == null ? null : Long.valueOf(paramMap.get("projectAmount"));
            boolean flag = false;
            if (inStorageAmount != null) {
                yearlyStatistics.setInStorageAmount(inStorageAmount);
                flag = true;
            }
            if (outStorageAmount != null) {
                yearlyStatistics.setOutStorageAmount(outStorageAmount);
                flag = true;
            }
            if (projectAmount != null) {
                yearlyStatistics.setProjectAmount(projectAmount);
                flag = true;
            }
            if (flag) {
                this.update(yearlyStatistics);
            }
        } catch (NumberFormatException e) {
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_PARAMS_ILLEGAL, "参数格式错误：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public void addInStorage(Integer year, ObjectType objectType, Long objectId) {
        if(year == null){
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_PARAMS_ILLEGAL, "请指定年份");
        }
        if(objectType == null || objectId == null){
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_PARAMS_ILLEGAL, "请指定对象");
        }
        Map<String, Object> params = new HashMap<>();
        Integer[] Year = new Integer[1];
        Year[0] = year;
        params.put("Year", Year);
        params.put("objectType", objectType);
        params.put("ObjectId", objectId);
        List<YearlyStatistics> list = this.getDao().listBy(params);
        if(list.size() > 1){
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_UNKNOWN_ERROR, "年度统计信息数据库存在重复记录");
        }else if(list.size() == 1){
            YearlyStatistics yearlyStatistics = list.get(0);
            yearlyStatistics.setInStorageAmount(yearlyStatistics.getInStorageAmount() + 1);
            this.update(yearlyStatistics);
        }else{
            YearlyStatistics yearlyStatistics = new YearlyStatistics();
            yearlyStatistics.setYear(year);
            yearlyStatistics.setInStorageAmount(1L);
            this.create(yearlyStatistics);
        }
    }

    @Override
    @Transactional
    public void addOutStorage(Integer year, ObjectType objectType, Long objectId) {
        if(year == null){
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_PARAMS_ILLEGAL, "请指定年份");
        }
        if(objectType == null || objectId == null){
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_PARAMS_ILLEGAL, "请指定对象");
        }
        Map<String, Object> params = new HashMap<>();
        Integer[] Year = new Integer[1];
        Year[0] = year;
        params.put("Year", Year);
        params.put("objectType", objectType);
        params.put("ObjectId", objectId);
        List<YearlyStatistics> list = this.getDao().listBy(params);
        if(list.size() > 1){
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_UNKNOWN_ERROR, "年度统计信息数据库存在重复记录");
        }else if(list.size() == 1){
            YearlyStatistics monthlyStatistics = list.get(0);
            monthlyStatistics.setOutStorageAmount(monthlyStatistics.getOutStorageAmount() + 1);
            this.update(monthlyStatistics);
        }else{
            YearlyStatistics monthlyStatistics = new YearlyStatistics();
            monthlyStatistics.setYear(year);
            monthlyStatistics.setOutStorageAmount(1L);
            this.create(monthlyStatistics);
        }
    }

    @Override
    @Transactional
    public void addProject(Integer year, ObjectType objectType, Long objectId) {
        if(year == null){
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_PARAMS_ILLEGAL, "请指定年份");
        }
        if(objectType == null || objectId == null){
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_PARAMS_ILLEGAL, "请指定对象");
        }
        Map<String, Object> params = new HashMap<>();
        Integer[] Year = new Integer[1];
        Year[0] = year;
        params.put("Year", Year);
        params.put("objectType", objectType);
        params.put("ObjectId", objectId);
        List<YearlyStatistics> list = this.getDao().listBy(params);
        if(list.size() > 1){
            throw new YearlyStatisticsBizException(YearlyStatisticsBizException.YEARLYSTATISTICSBIZ_UNKNOWN_ERROR, "年度统计信息数据库存在重复记录");
        }else if(list.size() == 1){
            YearlyStatistics monthlyStatistics = list.get(0);
            monthlyStatistics.setProjectAmount(monthlyStatistics.getProjectAmount() + 1);
            this.update(monthlyStatistics);
        }else{
            YearlyStatistics monthlyStatistics = new YearlyStatistics();
            monthlyStatistics.setYear(year);
            monthlyStatistics.setProjectAmount(1L);
            this.create(monthlyStatistics);
        }
    }
}
